package com.servlet.web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.servlet.bean.QueryVo;
import org.apache.commons.io.FileUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

/**
 * @Title: RequestServlet03
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/21 15:21
 */
@WebServlet("/req03")
public class RequestServlet03  extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 参数的接收 post的content-type  application/json
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        InputStream is = req.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
        String value="";
        String params="";
        while ( (params = br.readLine()) != null) {
            value += params;
        }

        QueryVo queryVo =  objectMapper.readValue(value, new TypeReference<QueryVo>() {
        });
        System.out.println(queryVo);
        resp.getWriter().write("success!");
    }


//    /**
//     * 参数的接收 post的content-type  application/json
//     * @param req
//     * @param resp
//     * @throws ServletException
//     * @throws IOException
//     */
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        InputStream is = req.getInputStream();
//
//
//        byte[] b = new byte[1024];
//        int length;
//        String value="";
//        while ( (length = is.read(b)) != -1) {
//            String params = new String(b, 0, length);
//            value += params;
//        }
//
//        QueryVo queryVo =  objectMapper.readValue(value, new TypeReference<QueryVo>() {
//        });
//        System.out.println(queryVo);
//        resp.getWriter().write("success!");
//    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
